home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 46
/
Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso
/
-serious-
/
comms
/
www
/
urlx
/
scanv.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-09-06
|
3KB
|
133 lines
/*
* scanv - scan voyager cache directory
*/
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <stdio.h>
#define getl_be(a) \
( ((u_long)(((u_char *)(a))[3])) \
+ ((u_long)(((u_char *)(a))[2]) << 8) \
+ ((u_long)(((u_char *)(a))[1]) << 16) \
+ ((u_long)(((u_char *)(a))[0]) << 24))
#define getw_be(a) \
( ((u_long)(((u_char *)(a))[1])) \
+ ((u_long)(((u_char *)(a))[0]) << 8))
char buf[1024];
char url[1024];
struct stat st;
void scan_dir(char *path, int pathlen)
{
DIR *dir;
struct direct *dirent;
int oldpathlen;
int is_voyager_dir = 0;
oldpathlen = pathlen;
if (!(dir = opendir(path)))
{
printf("couldn't open directory '%s'\n", path);
return;
}
if (pathlen >= 6 && strnicmp(path + pathlen - 6, "CIX", 3) == 0)
is_voyager_dir = 1;
#ifdef amigaos
if (pathlen && path[pathlen - 1] != ':' && path[pathlen - 1] != '/')
#endif
path[pathlen++] = '/';
while (dirent = readdir(dir))
{
memcpy(path + pathlen, dirent->d_name, dirent->d_namlen);
path[pathlen + dirent->d_namlen] = 0;
if (stat(path, &st) == 0)
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
{
scan_dir(path, pathlen + dirent->d_namlen);
}
else
{
/*
* file ops go here
*/
if (!is_voyager_dir)
continue;
/*
* open each file in turn and read url from header
*/
{
FILE *fp = 0;
if (fp = fopen(path, "rb"))
{
char temp[32];
int size, typelen, urllen;
/*
* offsets
* 4.l real size
* 8.w length of type description (follows url)
* 10.w length of url
* 140... url
*/
fread(temp, 16, 1, fp);
size = getl_be(temp + 4);
typelen = getw_be(temp + 8);
urllen = getw_be(temp + 10);
fseek(fp, 140, 0);
fread(url, urllen, 1, fp);
url[urllen] = 0;
fclose(fp);
printf("%s\n", url);
}
}
}
}
else
printf("no stat for %s\n", path);
}
path[oldpathlen] = 0;
closedir(dir);
/*
* directory ops would go here
*/
}
main(int argc, char **argv)
{
if (argc < 2)
{
printf("usage: scanv <voyagercachedirectoryname>\n");
exit(0);
}
strcpy(buf, argv[1]);
scan_dir(buf, strlen(buf));
return (0);
}